package CustomClassFile.CronJob;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import net.sf.json.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.StaticObj;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.interfaces.schedule.CronJob;

public class SyncZswsShift extends BaseCronJob implements CronJob {

	// 同步今天之后的所有排班数据，本地存在的排班则更新,不存在新增
	public void execute() {
		long startTime = System.currentTimeMillis(); // 获取开始时间

		// 查询hr视图今天之后的排班数据
		List<ZswsHRaShift> lists = new ArrayList<ZswsHRaShift>();
		JSONObject jsonObject = new JSONObject();
		String returnCode = "";
		String returnMessage = "";
		int add = 0;
		int update = 0;
		weaver.interfaces.datasource.DataSource ds = (weaver.interfaces.datasource.DataSource) StaticObj
				.getServiceByFullname(("datasource.HRDB"), weaver.interfaces.datasource.DataSource.class);
		java.sql.Connection conn = ds.getConnection();
		int datalength = 0;
		try {
			// 查询今天之后的排班
			String sql = "select eid,name,shift,convert(varchar(19),term,20) as term,title,xtype,agroupTitle,groupID from zswsHRaShift where convert(date,term,20) >convert(date,getdate(),20) order by eid ,term";
			PreparedStatement preparedStatement = conn.prepareStatement(sql);
			ResultSet resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {

				String eid = resultSet.getString("eid");
				String name = resultSet.getString("name");
				String shift = resultSet.getString("shift");
				String term = resultSet.getString("term");
				String title = resultSet.getString("title");
				String xtype = resultSet.getString("xtype");
				String agroupTitle = resultSet.getString("agroupTitle");
				String groupID = resultSet.getString("groupID");
				ZswsHRaShift zs = new ZswsHRaShift(eid, name, shift, term, title, xtype,agroupTitle,groupID);
				lists.add(zs);
				datalength++;
			}
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		// 查询本地数据库今天之后的排班,需要排除调班的排班
		String sql = "select * from uf_rypbxx where convert(date,term,20) >convert(date,getdate(),20) order by convert(int,eid),convert(date,term,20)";
		RecordSet rs = new RecordSet();
		rs.executeQuery(sql);

		List<ZswsHRaShift> listlocal = new ArrayList<ZswsHRaShift>();
		while (rs.next()) {
			String id = rs.getString("id");
			String eid = rs.getString("eid");
			String name = rs.getString("name");
			String shift = rs.getString("shift");
			String term = rs.getString("term");
			String title = rs.getString("title");
			String xtype = rs.getString("xtype");
			String agroupTitle = rs.getString("agroupTitle");
			String groupID = rs.getString("groupID");
			String tbflag=rs.getString("tbflag");
			ZswsHRaShift zsws = new ZswsHRaShift(id, eid, name, shift, term, title, xtype,agroupTitle,groupID,tbflag);
			listlocal.add(zsws);
		}
		// 判断今天之后的数据据是更新，还是有新增
		List<List> insertlist = new ArrayList<List>();
		List<List> updatelist = new ArrayList<List>();
		List<List> updatelist2 = new ArrayList<List>();
		for (ZswsHRaShift hrzsws : lists) {// 循环视图数据

			boolean flag = false;
			for (ZswsHRaShift localzsws : listlocal) {
				// 如果在本地找到相同人员的同一天的排班，则更新数据
				if (localzsws.getEid().equals(hrzsws.getEid()) && localzsws.getTerm().equals(hrzsws.getTerm())) {
					if ("1".equals(StringUtils.trim(localzsws.getTbflag()))) {//如果是调班,则不更新排班shift和title
						
						List<String> updateparam2 = new ArrayList<String>();					
						updateparam2.add("update:"+DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss"));
						updateparam2.add(hrzsws.getAgroupTitle());
						updateparam2.add(hrzsws.getGroupID());
						updateparam2.add(localzsws.getId());
						updatelist2.add(updateparam2);
					}else {
						
						List<String> updateparam = new ArrayList<String>();
						updateparam.add(hrzsws.getEid());
						updateparam.add(hrzsws.getName());
						updateparam.add(hrzsws.getShift());
						updateparam.add(hrzsws.getTerm());
						updateparam.add(hrzsws.getTitle());
						updateparam.add(hrzsws.getXtype());
						updateparam.add("update:"+DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss"));
						updateparam.add(hrzsws.getAgroupTitle());
						updateparam.add(hrzsws.getGroupID());
						updateparam.add(localzsws.getId());
						updatelist.add(updateparam);
					}
					flag = true;
					update++;
					break;
				}
			}
			if (!flag) {// 如果在本地找不到相同人员的同一天的排班，则添加数据
				List<String> insertparam = new ArrayList<String>();
				insertparam.add(hrzsws.getEid());
				insertparam.add(hrzsws.getName());
				insertparam.add(hrzsws.getShift());
				insertparam.add(hrzsws.getTerm());
				insertparam.add(hrzsws.getTitle());
				insertparam.add(hrzsws.getXtype());
				insertparam.add("insert:"+DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss"));
				insertparam.add(hrzsws.getAgroupTitle());
				insertparam.add(hrzsws.getGroupID());
				insertlist.add(insertparam);
				add++;
			}
		}
		RecordSet rs1 = new RecordSet();
		String insertbatch = "insert into uf_rypbxx(eid,name,shift,term,title,xtype,remarks,agroupTitle,groupID) values(?,?,?,?,?,?,?,?,?)";
		rs1.executeBatchSql(insertbatch, insertlist);
		
		RecordSet rs2 = new RecordSet();
		String updatebatch = "update uf_rypbxx set eid=?,name=?,shift=?,term=?,title=?,xtype=?,remarks=?,agroupTitle=?,groupID=? where id=? and (tbflag<>'1' or tbflag is null)";
		rs2.executeBatchSql(updatebatch, updatelist);

		RecordSet rs3 = new RecordSet();
		String updatebatch3 = "update uf_rypbxx set remarks=?,agroupTitle=?,groupID=? where id=? and tbflag='1'";
		rs3.executeBatchSql(updatebatch3, updatelist2);
		
		jsonObject.put("code", returnCode);
		jsonObject.put("message", returnMessage);
		jsonObject.put("add", add);
		jsonObject.put("update", update);
		jsonObject.put("total", datalength);
		long endTime = System.currentTimeMillis();
		long usetime = (endTime - startTime) / 1000;
		jsonObject.put("usetime", usetime + "s");
		BaseBean writelog = new BaseBean();
		writelog.writeLog("人员排班同步:"+jsonObject.toString());
	}
}
